package modele;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Collection;
import java.util.HashSet;
import java.util.TreeSet;

/**
 *
 * @author sio
 */
class DAO {

    static Collection<Dep> getLesDeps() {
        Collection<Dep> d = new HashSet<Dep>();
        try {
            Connection con = Connect.get();
            Statement req;
            req = con.createStatement();
            ResultSet rs = req.executeQuery("SELECT DISTINCT departement FROM medecin;");

            //Parcours
            String id;
            String nom;

            while (rs.next()) {
                id = rs.getString(1);
                Statement reqD = con.createStatement();
                Collection<Med> lesMeds = new TreeSet<Med>();
                ResultSet rsD = reqD.executeQuery("SELECT * FROM medecin WHERE departement=" + id);
                while (rsD.next()) {
                    Med m = new Med(rsD.getString("nom"), rsD.getString("prenom"), rsD.getString("adresse"),
                            rsD.getString("tel"), rsD.getString("specialitecomplementaire"), rsD.getString("id"));
                    lesMeds.add(m);
                }
                rsD.close();

                d.add(new Dep(id, lesMeds));

            }
            rs.close();

        } catch (SQLException e) {
            System.out.println("Erreur SQL :" + e);
        }
        return d;
    }
}
